<template>
  <el-dialog v-model="dialogVisible" :title="title" width="500" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="formRef">
      <el-form-item label="年级" :label-width="formLabelWidth" prop="class">
        <el-select v-model="form.class"  placeholder="请选择" clearable  style="width: 100%">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班号" :label-width="formLabelWidth" prop="number">
        <el-select v-model="form.number"  placeholder="请选择" clearable  style="width: 100%">
          <el-option v-for="i in 10" :key="i" :label="i" :value="i" />
        </el-select>
      </el-form-item>

      <el-form-item label="关联老师" :label-width="formLabelWidth" prop="teachers">
         

         <el-select
        v-model="form.teachers"
        multiple
        filterable
        remote
        reserve-keyword
        placeholder="请输入关联老师"
        :remote-method="remoteMethod"
        :loading="loading"
        style="width: 240px"
      >
        <el-option
          v-for="item in teacherList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="save(formRef)"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { ElMessageBox } from 'element-plus';
import { FormInstance, FormRules } from 'element-plus';

interface RuleForm {
  class: string;
  number: string;
  teachers: Array<string>;
}

const rules = reactive<FormRules<RuleForm>>({
  class: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
  number: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
  teachers: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
});
const props = defineProps({
  visiable: Boolean,
  action: String,
});
const emit1 = defineEmits(['updateVisiable']);
const formLabelWidth = '100px';
const formRef = ref();
const titleTxt = props.action === 'add' ? '添加班级信息' : '编辑班级信息';
const title = ref(titleTxt);
const dialogVisible = ref(props.visiable);
const form = reactive({
  class: '',
  number: '',
  teachers: [],
});
interface ListItem {
  value: string
  label: string
}
// const list = ref<ListItem[]>([])
const teacherList = ref<ListItem[]>([])
// const value = ref<string[]>([])
const loading = ref(false)

const options = ref([

  { value: '1', label: '1年级' },
  { value: '2', label: '2年级' },
  { value: '3', label: '3年级' },
  { value: '4', label: '4年级' },
  { value: '5', label: '5年级' },
]);


const remoteMethod = async (query: string) => {
  if (query) {
      loading.value = true
      await new Promise((resolve) => setTimeout(resolve, 500));
      loading.value = false;
      const list = [
        {label:"11",value:"11"},
        {label:"22",value:"22"},
        {label:"31",value:"31"},
        {label:"43",value:"43"},
        {label:"52",value:"52"},
        {label:"56",value:"56"},
        {label:"67",value:"67"},
        {label:"87",value:"87"},
        {label:"91",value:"91"},
      ]
      teacherList.value = list.filter((item) => {
        return item.label.toLowerCase().includes(query.toLowerCase())
      })
    
  } else {
    teacherList.value = []
  }
}
const handleClose = (done: () => void) => {
  emit1('updateVisiable', false);
};

const save = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!',form);
      if (props.action === 'add') {
        ElMessageBox.alert('新增成功', '提示', { type: 'success' });
      } else {
        ElMessageBox.alert('保存成功', '提示', { type: 'success' });
      }

      emit1('updateVisiable', false);
    } else {
      console.log('error submit!', fields);
    }
  });
};
</script>
